//
//  BaseController.m
//  adapter
//
//  Created by ac-hu on 2018/7/1.
//  Copyright © 2018年 ac-hu. All rights reserved.
//

#import "BaseController.h"

#define AnnimationTime              0.5
#define KEY_LOAD                    201316
#define margin                      10
#define maxWidth                    200
#define maxHeight                   300

@interface BaseController ()
@property (nonatomic,assign)CGFloat statusBarHeight;
@property (nonatomic,assign)BOOL isPush;
@end

@implementation BaseController

- (void)dealloc {

}

- (void)viewDidLoad{
    [super viewDidLoad];
    // 设置控制器view默认背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    // 适配ios7以前和ios以上的所有系统,该属性是苹果提供的适配属性,会自动检测控制器底部是否有导航栏，底部是否有TabBar
    if (ISP6) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count>0) {
        if ([viewcontrollers lastObject]==self) {
            self.isPush = YES;
        }else{
            self.isPush = NO;
        }
    }else{
       self.isPush = NO;
    }

    if (self.isPush) {
        if (viewcontrollers.count > 1) {
            [self showPushBackBtn];
//            ((BaseNav *)(self.navigationController)).naviBarStyle = 1;
        }
    }
}

- (BOOL)prefersStatusBarHidden
{
    return _hiddenStatusBar;
}

@end

#pragma mark - Category
@implementation BaseController (instance)

@end

@implementation BaseController (naviBarStyle)
// 设置导航栏背景
- (void)setNaviBarBgWithStyle:(KNavigationBarStyle)style {
    
    BaseNav *navi = (BaseNav *)self.navigationController;
    // 2.设置导航栏的背景图片
    NSString *navBarBg = nil;
    UIColor *color = nil;
    NSInteger statusBarStyle = UIStatusBarStyleDefault;
    UIBarButtonItem *barItem = [UIBarButtonItem appearance];
    navBarBg = iOS7_Greater ? @"NavBarWhite64" : @"NavBarWhite";
    color = ColorRGB(77, 77, 77);
    
    navi.naviBarStyle = KNavigationBarStyleWhite;
    if (IOS7_Less) {
        [UIApplication sharedApplication].statusBarStyle = statusBarStyle;
    }
    
    // 3.设置导航栏标题颜色
    [navi.navigationBar setTitleTextAttributes:@{
                                                 NSForegroundColorAttributeName : color,NSFontAttributeName : [UIFont systemFontOfSize:21.]
                                                 }];
    // 4.设置导航栏按钮文字颜色
    [barItem setTitleTextAttributes:@{
                                      NSForegroundColorAttributeName : color,
                                      NSFontAttributeName : [UIFont systemFontOfSize:13]
                                      } forState:UIControlStateNormal];
    // 5.设置导航栏背景图片
    //    [navi.navigationBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];
}
@end

@implementation BaseController (naviBarItemEvent)

// 返回
- (void)backButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backPresentClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//  参数:isPresent是代表是否是显示模态视图
-(void)showPushBackBtn
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString *title = @"返回";
    UIFont *font = [UIFont systemFontOfSize:16];
    [backBtn setFrame:CGRectMake(0, 0, [title wideTextFont:font], Height_Nav)];
    backBtn.title = title;
    backBtn.font = font.pointSize;
    backBtn.textColor = [UIColor darkGrayColor];
    [backBtn addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftbutton;
}

-(void)showPresentBackBtn
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString *title = @"返回";
    UIFont *font = [UIFont systemFontOfSize:16];
    [backBtn setFrame:CGRectMake(10, 0, [title wideTextFont:font], Height_Nav)];
    backBtn.title = title;
    backBtn.font = font.pointSize;
    [backBtn addTarget:self action:@selector(backPresentClick) forControlEvents:UIControlEventTouchUpInside];
}

@end

